package com.example.homework07.SQLite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.homework07.R;
import com.example.homework07.SQLite.Helper;

public class Sqlite extends Fragment {
    Helper dbHelper;
    String username;
    int age;
    String selection;
    public Sqlite(){
        super(R.layout.sqlite);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);
        dbHelper = new Helper(getContext());
        EditText editTextName = getView().findViewById(R.id.name);
        EditText editTextAge = getView().findViewById(R.id.age);
        TextView contentTextView = getView().findViewById(R.id.Show);
        Button tjButton = getView().findViewById(R.id.tianjia);
        Button xyButton = getView().findViewById(R.id.xiaoyu18);
        tjButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = editTextName.getText().toString();
                age = Integer.parseInt(editTextAge.getText().toString()) ;
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("username",username);
                values.put("age",age);
                long ret = db.insert("users",null,values);
                String msg = ret == -1 ? "添加失败！" : "添加成功";
                Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
                db.close();
                sy(contentTextView);
            }
        });

        xyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selection = "age<18";
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                StringBuilder stringBuilder = new StringBuilder();
                Cursor cursor = db.query("users",null,selection,null,null,null,null);
                while (cursor.moveToNext()){
                    long id = cursor.getLong(cursor.getColumnIndex("id"));
                    String username = cursor.getString(cursor.getColumnIndex("username"));
                    int age = cursor.getInt(cursor.getColumnIndex("age"));
                    stringBuilder.append("id: ").append(id)
                            .append(" username: ").append(username)
                            .append(" age: ").append(age)
                            .append("\n");
                }
                contentTextView.setText(stringBuilder.toString());
            }
        });
    }

    void sy(TextView contentTextView ){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        StringBuilder stringBuilder = new StringBuilder();
        Cursor cursor = db.query("users",null,"",null,null,null,null);
        while (cursor.moveToNext()){
            long id = cursor.getLong(cursor.getColumnIndex("id"));
            String username = cursor.getString(cursor.getColumnIndex("username"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            stringBuilder.append("id: ").append(id)
                    .append(" username: ").append(username)
                    .append(" age: ").append(age)
                    .append("\n");
        }
        contentTextView.setText(stringBuilder.toString());
    }
}
